package day06;

/**
 * @author Xiao
 * @date 2021/7/6
 */
public class ReverseArray {
    /*
        数组反转
            {1,3,5,7,9} → {9,7,5,3,1}
     */
    public static void main(String[] args) {
        int[] arr = {1, 3, 5, 7, 9};
        System.out.println("反转前数组：");
        printArray(arr);
        System.out.println("-------------");
        for (int i = 0; i < arr.length; i++) {
            int temp = arr[i];
            arr[i] = arr[arr.length - 1 - i];
            arr[arr.length - 1 - i] = temp;
            if ((arr.length - 1 - i) <= i) {
                break;
            }
        }
        System.out.println("反转后数组：");
        printArray(arr);
    }

    public static void printArray(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                System.out.print(arr[i]);
            } else {
                System.out.print(arr[i] + ", ");
            }
        }
        System.out.println("]");
    }
}
